Framework.Statistics = {}

local Stats = {}

STATISTIC_SINGLE	 = 1
STATISTIC_STACK 	 = 2
STATISTIC_COLLECTION = 3

function Framework.Statistics.New( ST_Name, EN_Type, OP_NU_Size )
	Stats[ST_Name] = { Type = EN_Type, Value = 0 }
	
	if( EN_Type == STATISTIC_STACK ) then
		Stats[ST_Name].Value = Framework.Objects.Stack( STACK_LILO | STACK_OVERFLOW, OP_NU_Size or nil )
	elseif( EN_Type == STATISTIC_COLLECTION ) then
		Stats[ST_Name].Value = {}
	end
end

function Framework.Statistics.Set( ST_Name, AY_Value, OP_ST_Index )
	if( Stats[ST_Name].Type == STATISTIC_STACK ) then
		Stats[ST_Name].Value:Push( AY_Value )
		return
	elseif( Stats[ST_Name].Type == STATISTIC_COLLECTION ) then
		if( OP_ST_Index ) then
			Stats[ST_Name].Value[OP_ST_Index] = AY_Value
		end
	end
	Stats[ST_Name].Value = AY_Value
end

function Framework.Statistics.Get( ST_Name, OP_ST_Index )
	if( Stats[ST_Name].Type == STATISTIC_STACK ) then
		return Stats[ST_Name].Value-1 -- Give them the last value
	elseif( Stats[ST_Name].Type == STATISTIC_COLLECTION ) then
		if( OP_ST_Index ) then
			return Stats[ST_Name].Value[OP_ST_Index]
		end
	end
	return Stats[ST_Name].Value
end

function Framework.Statistics.GetAverage( ST_Name, OP_FU_Add, OP_FU_Divide )
	if( Stats[ST_Name].Type == STATISTIC_STACK ) then
		return Stats[ST_Name].Value:Average( OP_FU_Add, OP_FU_Divide )
	end
	return Stats[ST_Name].Value -- Can't get the average of one value, now can ya?
end

function Framework.Statistics.GetHighest( ST_Name, OP_FU_Comparison )
	if( Stats[ST_Name].Type == STATISTIC_STACK ) then
		return Stats[ST_Name].Value:High( OP_FU_Comparison )
	end
	return Stats[ST_Name].Value -- Can't get the average of one value, now can ya?
end

function Framework.Statistics.GetLowest( ST_Name, OP_FU_Comparison )
	if( Stats[ST_Name].Type == STATISTIC_STACK ) then
		return Stats[ST_Name].Value:Low( OP_FU_Comparison )
	end
	return Stats[ST_Name].Value -- Can't get the average of one value, now can ya?
end

STATISTIC_PLOT_BARS = 1
STATISTIC_PLOT_PIE  = 2

if( CLIENT ) then
	function Framework.Statistics.Plot( ST_Name, EN_Type, NU_X, NU_Y, NU_W, NU_H )
		local Stat = Stats[ST_Name]
		if( Stat.Type == STATISTIC_SINGLE ) then return end
		if( EN_Type == STATISTIC_PLOT_BARS ) then
			Framework.Graph.Bars( ST_Name, Stat.Value, NU_X, NU_Y, NU_W, NU_H )
		elseif( EN_Type == STATISTIC_PLOT_PIE ) then
			Framework.Graph.Pie( ST_Name, Stat.Value, NU_X, NU_Y, NU_W, NU_H )
		end
	end
	
	function Framework.Statistics.Write( ST_Name, NU_X, NU_Y )
		surface.SetTextPos( NU_X, NU_Y )
		surface.DrawText( ST_Name .. ": " .. tostring(Framework.Statistics.Get( ST_Name )) )
	end
	
	
	Framework.Statistics.New( "Frames Per Second", STATISTIC_STACK, 100 )

	timer.Create( "FPSGrabber", 0.05, 0, function()
		Framework.Statistics.Set( "Frames Per Second", math.max( math.min( 300, 1 / RealFrameTime() ), 10 ) )
	end )
	
	/***************************************************************************************************************
	*** Statistic example using the Framework.Statistics.Plot function to display a bar-graph depicting FPS Data ***
	****************************************************************************************************************
	
	hook.Add( "HUDPaint", "FPSPainter", function()		
		surface.SetFont( "ChatFont" )
		surface.SetTextColor( 255, 255, 255, 255 )
		surface.SetDrawColor( 100, 255, 100, 255 )
		Framework.Statistics.Plot( "Frames Per Second", STATISTIC_PLOT_BARS, 30, 30, 300, 115 )
	end )
	*/
	
end


